package shiqi.util.stream;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @author shiqi
 * @version 1.0.0
 * @ClassName MethodReferenceTest2.java
 * @Description
 * @createTime 2022-05-06
 */
public class MethodReferenceTest2 {

    private static final List<Person> list;

    static {
        list = new ArrayList<>();
        list.add(new Person(19));
        list.add(new Person(18));
        list.add(new Person(20));
    }

    public static void main(String[] args) {
        System.out.println(list);
        // 改动2：既然Person内部有个逻辑一样的方法，就用它来替换Lambda
        list.sort(Person::compare);
        System.out.println(list);
    }

    @Data
    @AllArgsConstructor
    static class Person {
        private Integer age;

        // 改动1：新增一个方法，逻辑和之前案例的Lambda表达式相同
        public static int compare(Person p1, Person p2) {
            return p1.getAge() - p2.getAge();
        }
    }
}
